﻿using Lamex.Base;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lamex.mSA.Common
{
    public class MailHelper
    {
        public static void SendMailUseGmail(string subject,string[] strAdress,string[] strCC, DataTable dataTable)
        {
            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            foreach (string str in strAdress)
            {
                msg.To.Add(str);
            }
            foreach (string str in strCC)
            {
                msg.CC.Add(str);
            }
            //收件人增加自己
            msg.To.Add(Lamex.Common.Loginer.CurrentUser.Email);
            /*   
            * msg.CC.Add("c@c.com");   
            * msg.CC.Add("c@c.com");可以抄送给多人   
            */
            msg.From = new MailAddress(Lamex.Common.Loginer.CurrentUser.Email,"", System.Text.Encoding.UTF8);
            /* 上面3个参数分别是发件人地址，发件人姓名，编码*/
            msg.Subject = "请补齐CBM&Weight:" + subject + "";//邮件标题    
            msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码       
            msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码    
            msg.IsBodyHtml = true;//是否是HTML邮件    
            msg.Priority = MailPriority.Normal;//邮件优先级    



            //HTML格式
            //           string sql = @"SELECT top 10 a.TRANSPORT_NO as 'D/N NO' ,a.ORDER_NO AS 'SO NO' ,a.LINE_NO AS 'Item NO'  ,a.MATERIAL_NO AS 'SAP NO' ,'' AS '分厂'
            //FROM Transfer_SA_DN_List a";
            //           DataTable data = BaseBuildSql.GetDataTableBySql(sql);
            //添加新列，其值为默认值
            DataTable dataTable1 = new DataTable();
            dataTable1 = dataTable.Copy();
            DataColumn dc1 = new DataColumn("CBM单位", typeof(string));
            dc1.DefaultValue = "m³";
            DataColumn dc2 = new DataColumn("重量单位", typeof(string));
            dc2.DefaultValue = "KG";
            dataTable1.Columns.Add(dc1);
            dataTable1.Columns.Add(dc2);
            DataView dataView = dataTable1.DefaultView;
            DataTable data = dataView.ToTable(true, new string[] { "TRANSPORT_NO", "ORDER_NO", "LINE_NO", "MATERIAL_NO", "MATERIAL_DESC", "MODEL_NO", "PRODUCT_LINE", "SHIPPING_VOLUME", "CBM单位", "SHIPPING_WEIGHT", "重量单位" });
            data.Columns["TRANSPORT_NO"].ColumnName = "D/N";
            data.Columns["ORDER_NO"].ColumnName = "SO";
            data.Columns["LINE_NO"].ColumnName = "Item";
            data.Columns["MATERIAL_NO"].ColumnName = "SAP NO";
            data.Columns["MATERIAL_DESC"].ColumnName = "产品描述";
            data.Columns["MODEL_NO"].ColumnName = "型号";
            data.Columns["PRODUCT_LINE"].ColumnName = "分厂";
            data.Columns["SHIPPING_VOLUME"].ColumnName = "CBM";
            data.Columns["SHIPPING_WEIGHT"].ColumnName = "重量";
        


            //附件  
            string file = @"D:\MESTemp\CBM_" + subject + "_" + DateTime.Now.ToString("yyyyMMdd_hhmmss") + ".xlsx";
            if (Common.ExcelHelpers.DataTableToExcel(data, file, false))
            {
                Attachment att = null;
                if (File.Exists(file))
                {
                    att = new Attachment(file);
                    msg.Attachments.Add(att);
                    //释放流
                    FileStream fs = File.OpenRead(file);//打开文件读取
                    fs.Flush();
                    fs.Dispose();
                    fs.Close();//释放流

                }
            }


            string MailBody = "Dear ALL,";
            MailBody += "<br>";
            MailBody += "&nbsp;&nbsp;&nbsp以下出货通知单包含无CBM/Weight的产品影响排车,请于17：30前或最迟于明日早上9:30前于SAP系统内补齐以下产品的CBM和Weight,<br>";
            MailBody += "并同步邮件答复相关人员, 以便即时进行车程安排。<br>";
            MailBody += "<table cellspacing=\"1\" cellpadding=\"3\" border=\"1\"  style=\"font-size: 10pt;line-height: 15px;\">";
            MailBody += "<div align=\"center\">";
            MailBody += "<tr>";
            //处理表头格式
            for (int hcol = 0; hcol < data.Columns.Count; hcol++)
            {
                //MailBody += "<td bgcolor=\"436EEE\" style=\"font-size: 13pt\">&nbsp;&nbsp;&nbsp;";
                //MailBody += data.Columns[hcol].ColumnName;
                //MailBody += "&nbsp;&nbsp;&nbsp;</td>";

                MailBody += "<td bgcolor=\"436EEE\" style=\"font-size: 13pt\">";
                MailBody += data.Columns[hcol].ColumnName;
                MailBody += "</td>";
            }
            MailBody += "</tr>";

            //处理表格内容
            for (int row = 0; row < data.Rows.Count; row++)
            {
                MailBody += "<tr>";
                for (int col = 0; col < data.Columns.Count; col++)
                {
                    //MailBody += "<td bgcolor=\"FFFFFF\">&nbsp;&nbsp;&nbsp;";
                    //MailBody += data.Rows[row][col].ToString();
                    //MailBody += "&nbsp;&nbsp;&nbsp;</td>";

                    MailBody += "<td bgcolor=\"FFFFFF\">";
                    MailBody += data.Rows[row][col].ToString();
                    MailBody += "</td>";
                }
                MailBody += "</tr>";
            }
            MailBody += "</table>";
            MailBody += "</div>";
            //增加署名
            MailBody += "<br>";
            MailBody += "Best Regards,";
            MailBody += "<br>";
            MailBody += Lamex.Common.Loginer.CurrentUser.AccountName;
            MailBody += "<br>";
            MailBody +="联系电话："+ Lamex.Common.Loginer.CurrentUser.Tel;
            msg.Body = MailBody;



            SmtpClient client = new SmtpClient();
            client.Credentials = new System.Net.NetworkCredential("mailsender@lamex.com.hk", "Bl@7701167//");
            //client.Credentials = new System.Net.NetworkCredential("Bill.Luo@lamex.com.hk", "");
            client.Host = "172.23.134.118";
            // client.EnableSsl = true;//经过ssl加密    
            object userState = msg;
            try
            {
                client.Send(msg);
                GC.Collect();

            }
            catch (System.Net.Mail.SmtpException ex)
            {
                MessageBox.Show(ex.Message, "发送邮件出错");
            }
        }
        public static void SendMail_2(string[] strAdress, string[] strCC, DataTable data)
        {
            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            foreach (string str in strAdress)
            {
                msg.To.Add(str);
            }
            foreach (string str in strCC)
            {
                msg.CC.Add(str);
            }
            //收件人增加自己
            msg.To.Add(Lamex.Common.Loginer.CurrentUser.Email);
            /*   
            * msg.CC.Add("c@c.com");   
            * msg.CC.Add("c@c.com");可以抄送给多人   
            */
            msg.From = new MailAddress(Lamex.Common.Loginer.CurrentUser.Email, "", System.Text.Encoding.UTF8);
            /* 上面3个参数分别是发件人地址，发件人姓名，编码*/
            msg.Subject = "物料重量未维护清单";//邮件标题    
            msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码       
            msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码    
            msg.IsBodyHtml = true;//是否是HTML邮件    
            msg.Priority = MailPriority.Normal;//邮件优先级    



            //HTML格式
            //           string sql = @"SELECT top 10 a.TRANSPORT_NO as 'D/N NO' ,a.ORDER_NO AS 'SO NO' ,a.LINE_NO AS 'Item NO'  ,a.MATERIAL_NO AS 'SAP NO' ,'' AS '分厂'
            //FROM Transfer_SA_DN_List a";
            //           DataTable data = BaseBuildSql.GetDataTableBySql(sql);              
            //DataTable data = dataView.ToTable(true, new string[] { "TRANSPORT_NO", "ORDER_NO", "LINE_NO", "MATERIAL_NO", "MATERIAL_DESC", "MODEL_NO", "PRODUCT_LINE", "SHIPPING_VOLUME", "CBM单位", "SHIPPING_WEIGHT", "重量单位" });
            //data.Columns["TRANSPORT_NO"].ColumnName = "D/N";
            //data.Columns["ORDER_NO"].ColumnName = "SO";
            //data.Columns["LINE_NO"].ColumnName = "Item";
            //data.Columns["MATERIAL_NO"].ColumnName = "SAP NO";
            //data.Columns["MATERIAL_DESC"].ColumnName = "产品描述";
            //data.Columns["MODEL_NO"].ColumnName = "型号";
            //data.Columns["PRODUCT_LINE"].ColumnName = "分厂";
            //data.Columns["SHIPPING_VOLUME"].ColumnName = "CBM";
            //data.Columns["SHIPPING_WEIGHT"].ColumnName = "重量";



            ////附件  
            //string file = @"D:\MESTemp\CBM_" + subject + "_" + DateTime.Now.ToString("yyyyMMdd_hhmmss") + ".xlsx";
            //if (Common.ExcelHelpers.DataTableToExcel(data, file, false))
            //{
            //    Attachment att = null;
            //    if (File.Exists(file))
            //    {
            //        att = new Attachment(file);
            //        msg.Attachments.Add(att);
            //        //释放流
            //        FileStream fs = File.OpenRead(file);//打开文件读取
            //        fs.Flush();
            //        fs.Dispose();
            //        fs.Close();//释放流

            //    }
            //}


            string MailBody = "Dear ALL,";
            MailBody += "<br>";
            MailBody += "&nbsp;&nbsp;&nbsp请在2小时内维护提供以下数据，谢谢！<br>";
            MailBody += "<table cellspacing=\"1\" cellpadding=\"3\" border=\"1\"  style=\"font-size: 10pt;line-height: 15px;\">";
            MailBody += "<div align=\"center\">";
            MailBody += "<tr>";
            //处理表头格式
            for (int hcol = 0; hcol < data.Columns.Count; hcol++)
            {
                //MailBody += "<td bgcolor=\"436EEE\" style=\"font-size: 13pt\">&nbsp;&nbsp;&nbsp;";
                //MailBody += data.Columns[hcol].ColumnName;
                //MailBody += "&nbsp;&nbsp;&nbsp;</td>";

                MailBody += "<td bgcolor=\"436EEE\" style=\"font-size: 13pt\">";
                MailBody += data.Columns[hcol].ColumnName;
                MailBody += "</td>";
            }
            MailBody += "</tr>";

            //处理表格内容
            for (int row = 0; row < data.Rows.Count; row++)
            {
                MailBody += "<tr>";
                for (int col = 0; col < data.Columns.Count; col++)
                {
                    //MailBody += "<td bgcolor=\"FFFFFF\">&nbsp;&nbsp;&nbsp;";
                    //MailBody += data.Rows[row][col].ToString();
                    //MailBody += "&nbsp;&nbsp;&nbsp;</td>";

                    MailBody += "<td bgcolor=\"FFFFFF\">";
                    MailBody += data.Rows[row][col].ToString();
                    MailBody += "</td>";
                }
                MailBody += "</tr>";
            }
            MailBody += "</table>";
            MailBody += "</div>";
            //增加署名
            MailBody += "<br>";
            MailBody += "Best Regards,";
            MailBody += "<br>";
            MailBody += Lamex.Common.Loginer.CurrentUser.AccountName;
            MailBody += "<br>";
            MailBody += "联系电话：" + Lamex.Common.Loginer.CurrentUser.Tel;
            msg.Body = MailBody;



            SmtpClient client = new SmtpClient();
            client.Credentials = new System.Net.NetworkCredential("mailsender@lamex.com.hk", "Bl@7701167//");
            //client.Credentials = new System.Net.NetworkCredential("Bill.Luo@lamex.com.hk", "");
            client.Host = "172.23.134.118";
            // client.EnableSsl = true;//经过ssl加密    
            object userState = msg;
            try
            {
                client.Send(msg);
                GC.Collect();

            }
            catch (System.Net.Mail.SmtpException ex)
            {
                MessageBox.Show(ex.Message, "发送邮件出错");
            }
        }

    }
}
